<?php

class CommentsController extends \BaseController {

    /**
     * Display a listing of the resource.
     *
     * @param $video_id
     * @return Response
     */
    public function getShow($video_id)
    {
        $nerds = Comments::where('video_id', '=', $video_id)
            ->join('profile', 'comments.user_id', '=', 'profile.user_id')
            ->select('comments.id','comments.video_id','comments.user_id','comments.text','comments.created_at','profile.fullname','profile.profile_picture')
            ->orderBy('created_at', 'desc')
            ->paginate(5);

        return Response::json(array('status' => 'success', 'state' => $nerds->toArray()), 200);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return Response
     */
//    public function create()
//    {
//        return View::make('upload.upload', array('num' => str_random(8)));
//    }

    /**
     * Store a newly created resource in storage.
     *
     * @return Response
     */
    public function postCreate()
    {
        // validate
        // read more on validation at http://laravel.com/docs/validation
        $rules = array(
            'text'       => 'required',
            'video_id'   => 'required|numeric'
        );
        $validator = Validator::make(Input::all(), $rules);

        // process the login
        if ($validator->fails()) {
            return Response::json('failed', 200);
        } else {
            // store
            $comment = new Comments;
            $comment->video_id = Input::get('video_id');
            $comment->user_id = 1;
            $comment->text = Input::get('text');
            // TODO: 这里的user_id 对应登录的人，需要重新修改
            $comment->created_time = time();
            $comment->save();
            return Response::json('success', 200); // or do a redirect with some message that file was uploaded
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return Response
     */
//    public function getShow($id)
//    {
//        // get the nerd
//        $nerd = Comments::where('id','=',$id)
//                    ->join('profile', 'comments.user_id', '=', 'profile.user_id')
//                    ->select('comments.id','comments.video_id','comments.user_id','comments.text','comments.created_at','profile.fullname','profile.profile_picture')
//                    ->get();
//
//        // show the view and pass the nerd to it
//        return Response::json(array('status' => 'success', 'state' => $nerd->toArray()), 200);
//    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return Response
     */
    public function getEdit($id)
    {
        return Response::json(array('status' => 'success', 'state' => "edit page"), 200);
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  int  $id
     * @return Response
     */
    public function postUpdate($id)
    {
        // validate
        // read more on validation at http://laravel.com/docs/validation
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @return Response
     */
    public function postDestroy()
    {
        $rules = array(
            'id'   => 'required',
        );
        $validator = Validator::make(Input::all(), $rules);

        // process the login
        if ($validator->fails()) {
            return Response::json('failed', 200);
        }
        // delete
        $nerd = Comments::find(Input::get('id'));
        $nerd->delete();

        // redirect
        return Response::json('success', 200);
    }
}